package com.yjetms.service;

import com.yjetms.entity.Data;
import com.yjetms.entity.Stuffbasic;
import com.yjetms.repository.StuffRepository;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import java.util.List;

public class StuffbasicServiceImpl implements StuffbasicService{

    StuffRepository stuffRepository;

    public StuffbasicServiceImpl(StuffRepository stuffRepository) {
        this.stuffRepository = stuffRepository;
    }

    @Override
    public boolean save(Stuffbasic stuffbasic) {
        stuffRepository.save(stuffbasic);
        return true;
    }

    @Override
    public boolean existStuff(String IDCard) {
        Stuffbasic stuffbasic = stuffRepository.findByIDcard(IDCard);
        if(stuffbasic==null)
            return false;
        return true;
    }

    @Override
    public Data<Stuffbasic> searchpage(Stuffbasic stuffbasic,Integer page,Integer limit) {
        //stuffRepository.findAll()
        PageRequest pageRequest=PageRequest.of(page-1,limit);
        Example<Stuffbasic> example=Example.of(stuffbasic);
        Page<Stuffbasic> pagelist = stuffRepository.findAll(example, pageRequest);
        List<Stuffbasic> list=pagelist.getContent();
        long count = stuffRepository.count(example);
        int dataNum=(int)count;
        Data<Stuffbasic> pageData=new Data<>(0,"",dataNum,list);

        return pageData;
    }
}
